/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description router全局配置，如有必要可分文件抽离
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layouts'
import EmptyLayout from '@/layouts/EmptyLayout'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true,
  },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/401'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true,
  },
]

/*当settings.js里authentication配置的是intelligence时，views引入交给前端配置*/
export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'Index',
        component: () => import('@/views/index/index'),
        meta: {
          title: '首页',
          icon: 'home',
          affix: true,
          noKeepAlive: true,
        },
      },
    ],
  },
  {
    path: '/user',
    component: Layout,
    redirect: 'noRedirect',
    name: 'User',
    alwaysShow: true,
    meta: {
      title: '用户功能',
      permissions: ['manager', 'editor', 'admin'],
      icon: 'user',
    },
    children: [
      {
        path: '/user/userTable',
        name: 'UserTable',
        component: () => import('@/views/user/userTable/index'),
        meta: {
          title: '所有用户',
          icon: 'users',
          permissions: ['manager'],
        },
      },
      {
        path: '/user/personalCenter',
        name: 'PersonalCenter',
        component: () => import('@/views/user/personal/index'),
        meta: {
          title: '个人中心',
          icon: 'users',
          permissions: ['editor', 'admin'],
        },
      },
    ],
  },
  {
    path: '/appoint',
    component: Layout,
    redirect: 'noRedirect',
    name: 'Appoint',
    alwaysShow: true,
    meta: {
      title: '预约功能',
      permissions: ['editor', 'manager'],
      icon: 'clock',
    },
    children: [
      {
        path: '/appointment/appoint',
        name: 'AppointOnline',
        component: () => import('@/views/appointment/appoint/index'),
        meta: {
          title: '在线预约',
          icon: 'clock',
          permissions: ['editor'],
        },
      },
      {
        path: '/appointment/myappoint',
        name: 'MyAppoint',
        component: () => import('@/views/appointment/myappoint/index'),
        meta: {
          title: '我的预约',
          icon: 'clock',
          permissions: ['editor'],
        },
      },
    ],
  },
  {
    path: '/doctor',
    component: Layout,
    redirect: 'noRedirect',
    name: 'Doctor',
    alwaysShow: true,
    meta: {
      title: '医生功能',
      permissions: ['admin', 'manager'],
      icon: 'user',
    },
    children: [
      {
        path: '/doctor/doctorTable',
        name: 'DoctorTable',
        component: () => import('@/views/doctor/doctorTable/index'),
        meta: {
          title: '所有医生',
          icon: 'users',
          permissions: ['manager'],
        },
      },
      {
        path: '/appointment/appointList',
        name: 'History',
        component: () => import('@/views/appointment/history/index'),
        meta: {
          title: '预约管理',
          icon: 'clock',
          permissions: ['admin', 'manager'],
        },
      },
    ],
  },
  {
    path: '/history',
    component: Layout,
    redirect: 'noRedirect',
    name: 'History',
    // alwaysShow: true,
    meta: {
      title: '接种记录',
      permissions: ['admin', 'manager', 'editor'],
      icon: 'history',
      noKeepAlive: true,
    },
    children: [
      {
        path: '/history/history',
        name: 'History',
        component: () => import('@/views/history/index'),
        meta: {
          title: '接种记录',
          icon: 'history',
          noKeepAlive: true,
          permissions: ['admin', 'manager', 'editor'],
        },
      },
    ],
  },
  {
    path: '/vaccines',
    component: Layout,
    redirect: 'noRedirect',
    name: 'Vaccines',
    // alwaysShow: true,
    meta: {
      title: '疫苗信息',
      permissions: ['admin', 'manager', 'editor'],
      icon: 'history',
    },
    children: [
      {
        path: '/vaccines/vaccine',
        name: 'History',
        component: () => import('@/views/vaccines/index'),
        meta: {
          title: '疫苗信息',
          icon: 'history',
          permissions: ['admin', 'manager', 'editor'],
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
  },
]

const router = new VueRouter({
  base: routerMode === 'history' ? publicPath : '',
  mode: routerMode,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
})
//注释的地方是允许路由重复点击，如果你觉得框架路由跳转规范太过严格可选择放开
/* const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
}; */

export function resetRouter() {
  router.matcher = new VueRouter({
    base: routerMode === 'history' ? publicPath : '',
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: constantRoutes,
  }).matcher
}

export default router
